//
//  HeZhuoSheViewController.m
//  ZhuoxinAgriculture
//
//  Created by 左忠飞 on 16/11/9.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "HeZhuoSheViewController.h"
#import "HeZhuoSheCell.h"
#import "Masonry.h" 
#import "ZuzhiDetail.h"
#import "HeZhuoSheDetailController.h"


#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

#define kBILI (kScreenW/750.0)
#define kI(v) (v*kBILI)
#define UIColorFromRGBA(rgbValue) \
[UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:1.0]

#define lazyLoad(type, name, methodName) - (type *)methodName\
{\
    if (!name) {\
        name = [type new];\
    }\
    return name;\
}
@interface HeZhuoSheViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *headerIcon; // 180*180

@property (nonatomic, strong) UIButton *headerIconBtn; // 180*180

@property (nonatomic, strong) UIView *describeView;
@property (nonatomic, strong) UILabel *hzsNameLabel;
@property (nonatomic, strong) UILabel *peopleCountLabel;
@property (nonatomic, strong) UITableView *tbView;
@property (nonatomic, strong) NSArray <NSDictionary *>*dataSource;
@property (nonatomic, strong) UILabel *navTitle;
@property (nonatomic, strong) UIButton *popBtn;
@end

@implementation HeZhuoSheViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"社员列表";
    
    [self setupUI];
    
}

- (void)setupUI
{
    [self.view addSubview:self.tbView];
    
    
    self.tbView.tableHeaderView = self.headerView;
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.left.height.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(-20.0);
    }];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView).offset(kI(150.0));
        make.centerX.mas_equalTo(self.headerView);
        make.height.width.mas_equalTo(kI(180.0));
    }];
    
    [self.headerIconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView).offset(kI(150.0));
        make.centerX.mas_equalTo(self.headerView);
        make.height.width.mas_equalTo(kI(180.0));
    }];
    
    [self.describeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerIcon.mas_bottom).offset(16);
        make.centerX.mas_equalTo(self.headerIcon);
//        make.height.mas_equalTo(self.hzsNameLabel);
//        make.left.mas_equalTo(self.hzsNameLabel);
//        make.right.mas_equalTo(self.peopleCountLabel);
    }];
    
    [self.hzsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.describeView);
        make.top.mas_equalTo(self.describeView);
        make.bottom.mas_equalTo(self.describeView);
    }];
    
    [self.peopleCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.hzsNameLabel.mas_right).offset(8.0);
        make.right.mas_equalTo(self.describeView);
        make.top.mas_equalTo(self.describeView);
    }];
    
    [self.navTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView).offset(40);
        make.centerX.mas_equalTo(self.headerView);
    }];
    
    [self.popBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView).offset(10.0);
        make.centerY.mas_equalTo(self.navTitle);
        make.width.height.mas_equalTo(kI(42.0));
    }];
    
}

#pragma mark - tableView datasource/delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HeZhuoSheCell *cell = [HeZhuoSheCell HZSCellWithTableView:tableView];
    
    cell.cellData = self.dataSource[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return kI(200.0);
    return 112;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //跳转小农的详情
    ZuzhiDetail *zuzhi = [[ZuzhiDetail alloc] init];
    zuzhi.number = @"小农";
    [self.navigationController pushViewController:zuzhi animated:YES];
}


#pragma mark - 懒加载
- (UITableView *)tbView
{
    if (!_tbView) {
        
        _tbView = [UITableView new];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.rowHeight = 112;
        _tbView.tableFooterView = [UIView new];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.bounds = CGRectMake(0, 0, kScreenW, kI(416.0));
        _headerView.backgroundColor = [UIColor cyanColor];
        [_headerView addSubview:self.headerIcon];
        [_headerView addSubview:self.headerIconBtn];
        [_headerView addSubview:self.describeView];
        [_headerView addSubview:self.hzsNameLabel];
        [_headerView addSubview:self.peopleCountLabel];
        [_headerView addSubview:self.popBtn];
        [_headerView addSubview:self.navTitle];
        self.hzsNameLabel.text = @"罗庄合作社";
        self.peopleCountLabel.backgroundColor = [UIColor redColor];
        self.peopleCountLabel.textColor = [UIColor whiteColor];
        self.peopleCountLabel.text = @"3人";
    }
    return _headerView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.alpha = 0.0;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.alpha = 1;
}

- (NSArray<NSDictionary *> *)dataSource
{
    if (!_dataSource) {
        _dataSource = @[@{@"icon":@"01-sili",
                          @"name":@"李四",
                          @"contact":@"电话: 18823412980",
                          @"No":@"(1234567)",
                          @"identity":@"小农",
                          @"org":@"罗庄合作社"},
                        @{@"icon":@"01-wangwu",
                          @"name":@"王五",
                          @"contact":@"电话: 18823412980",
                          @"No":@"(1234567)",
                          @"identity":@"小农",
                          @"org":@"罗庄合作社"},
                        @{@"icon":@"01-zhangsan",
                          @"name":@"张三",
                          @"contact":@"电话: 18823412980",
                          @"No":@"(1234567)",
                          @"identity":@"小农",
                          @"org":@"罗庄合作社"}
                        ];
    }
    return _dataSource;
}

- (UIView *)headerIcon
{
    if (!_headerIcon) {
        _headerIcon = [UIView new];
        _headerIcon.layer.contents = (__bridge id _Nullable)([[UIImage imageNamed:@"合作社头像"] CGImage]);
    }
    return _headerIcon;
}
- (UIButton *)headerIconBtn
{
    if (!_headerIconBtn) {
        _headerIconBtn = [[UIButton alloc]init];
        _headerIconBtn.backgroundColor = [UIColor clearColor];
        [_headerIconBtn addTarget:self action:@selector(headerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerIconBtn;
}
//透明的button,点击跳转合作社详情页.
- (void)headerBtnClick{
    HeZhuoSheDetailController *hzsVc = [[UIStoryboard storyboardWithName:@"HeZhuoSheDetailController" bundle:nil] instantiateInitialViewController];
    [self.navigationController pushViewController:hzsVc animated:YES];
}


- (UIView *)describeView
{
    if (!_describeView) {
        _describeView = [UIView new];
    }
    return _describeView;
}

lazyLoad(UILabel, _hzsNameLabel, hzsNameLabel);

lazyLoad(UILabel, _peopleCountLabel, peopleCountLabel);

- (UILabel *)navTitle
{
    if (!_navTitle) {
        _navTitle = [UILabel new];
        _navTitle.font = [UIFont systemFontOfSize:16.0];
        _navTitle.text = @"社员列表";
    }
    return _navTitle;
}

- (UIButton *)popBtn
{
    if (!_popBtn) {
        _popBtn = [UIButton new];
        [_popBtn addTarget:self action:@selector(popBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_popBtn setImage:[UIImage imageNamed:@"01-icon---back"] forState:UIControlStateNormal];
    }
    return _popBtn;
}

- (void)popBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
